package com.suneee.cloud.erpInterface.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suneee.cloud.erpInterface.bean.OrderBean;
import com.suneee.cloud.erpInterface.service.OrderService;
import com.suneee.core.error.Assert;
import com.suneee.enterprise.dao.EnterpriseDao;
import com.suneee.enterprise.entity.Enterprise;
import com.suneee.trades.bean.SendGoodsDTO;
import com.suneee.trades.bean.TradesInfoDTO;
import com.suneee.trades.dao.TradesInfoDao;
import com.suneee.trades.dao.TradesOrderDao;
import com.suneee.trades.entity.TradesInfo;
import com.suneee.trades.entity.TradesOrder;
import com.suneee.trades.service.TradesInfoService;

@Service("api-orderService")
public class OrderServiceImpl implements OrderService{

	@Autowired
	private TradesInfoDao tradesInfoDao;
	
	@Autowired
	private TradesOrderDao tradesOrderDao;
	
	@Autowired
	private EnterpriseDao enterpriseDao;
	
	@Autowired
	private TradesInfoService tradesInfoService;
	@Override
	public List<TradesInfo> sendOrderInfo() {
		List<TradesInfo> spuBeanList = new ArrayList<TradesInfo>();  //返回数据集合
		
		//获取所有入驻中台的企业信息
		Object[] args = new Object[1];
		args[0] = "1";
		List<Enterprise> enterpriseList = enterpriseDao.selectWhere(" enabled=? and out_mall_code is not null", args);
		
		for (Enterprise enterprise : enterpriseList) {
			List<TradesInfo> tradesInfoList = tradesInfoDao.queryNotSynTradesInfo(enterprise.getEnterpriseCode());
			
			
			if(tradesInfoList != null && tradesInfoList.size() > 0){
				//定义发送数据的bean
				
				for (TradesInfo tradesInfo : tradesInfoList) {
					//查询订单行信息
					List<TradesOrder> tradesOrderList= tradesOrderDao.getNotSynTradesOrderByTid(tradesInfo.getTid());
					if(tradesOrderList != null && tradesOrderList.size() > 0){
						List<TradesOrder> orderList= new ArrayList<TradesOrder>();//返回订单行数据集合
						for (TradesOrder tradesOrder : tradesOrderList) {
							
							orderList.add(tradesOrder);
						}
						tradesInfo.setTradesOrderList(orderList);
					}
					
					spuBeanList.add(tradesInfo);
				}
			}
		}
		
		return spuBeanList;
	}


	
	
	@Override
	public TradesInfo getOrderDetail(String tid) {
		
		return null;
	}




	@Override
	public void returnDelivery(OrderBean bean) {
		Assert .notNull(bean.getTid(), "订单号不能为空");
		Assert .notNull(bean.getWaybill(), "运单号不能为空");
		
		//记录回传打单发货
		TradesInfoDTO tradesInfoDTO=new TradesInfoDTO();
		List<SendGoodsDTO>goods=new ArrayList<SendGoodsDTO>();
		SendGoodsDTO sendGoodsDTO=new SendGoodsDTO();
		sendGoodsDTO.setOutSid(bean.getWaybill());
		sendGoodsDTO.setShoppId(Integer.parseInt(bean.getTid()));
		goods.add(sendGoodsDTO);
		tradesInfoDTO.setSendGoods(goods);
		tradesInfoService.selectSendGoods(tradesInfoDTO);
	}




}
